import java.util.ArrayList;
import java.util.List;

public class dfs {
    public static void main(String[] args) {
        int[] arr = {1, 2, 2};
        dfs(arr, 0);
        System.out.println(list);
    }

    static List<List<Integer>> list = new ArrayList<>();
    static List<Integer> tmp = new ArrayList<>();
    static boolean[] falg = new boolean[3];
    public static void dfs(int[] arr, int start) {
        if(tmp.size()==arr.length)
        list.add(new ArrayList<>(tmp));
        for (int i = start; i < arr.length; i++) {
            if(falg[i]) continue;
            if(i>0&&arr[i]==arr[i-1]&&!falg[i-1])continue;
            falg[i] = true;
            tmp.add(arr[i]);
            dfs(arr,i+1);
            falg[i] = false;
            tmp.remove(tmp.size() - 1);
        }
    }

}
